package com.recsmile.sgs.ui.components;

import com.recsmile.sgs.core.Player;
import com.recsmile.sgs.entity.*;
import com.recsmile.sgs.entity.cards.basic.Attack;
import com.recsmile.sgs.listener.GameListener;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import static sun.audio.AudioPlayer.player;

/**
 * Created by Administrator on 2016/8/25.
 */
public class GamePanelGui extends JPanel implements GameListener{
    public static final int WIDTH = 1600;
    public static final int HEIGHT = 900;

    private CardRackGui cardRack;
    private EquipmentRackGui equipmentRack;
    private HeroGui heroGui;
    private LifebarGui healthGui;
    private CardDisposalGui disposalGui;
    private CardSelectionPane pane;
    private JPanel customizedPanel;

    private SelfPlayerSimple myself;
    private List<PlayerGui> otherPlayers;
    private ButtonGui confirm;
    private ButtonGui cancel;
    private ButtonGui end;

    private JLabel deckSize;
    private MessageBoxGui messageBox;

    private final GamePanel panel;

    private int cancelSetCount;

    public GamePanelGui(Player player, GamePanel panel) {
        this.panel = panel;
        setLayout(new BorderLayout());
        myself = new SelfPlayerSimple(player.getNickname(), player.getLocation());

        cardRack = new CardRackGui(panel);
        equipmentRack = new EquipmentRackGui(panel);
        heroGui = new HeroGui(panel);
        healthGui = new LifebarGui();
        disposalGui = new CardDisposalGui(this);
        otherPlayers = new ArrayList<PlayerGui>();
        deckSize = new JLabel();
        deckSize.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));
        deckSize.setSize(100, 100);
        deckSize.setLocation(WIDTH - 100, PlayerGui.HEIGHT);
        messageBox = new MessageBoxGui();
        messageBox.setLocation(equipmentRack.getWidth(), HEIGHT - cardRack.getHeight() - MessageBoxGui.HEIGHT);
        pane = null;

        myself.registerGameListener(this);
        myself.registerCardOnHandListener(cardRack);
        myself.registerEquipmentListener(equipmentRack);
        myself.registerHealthListener(healthGui);
        myself.registerCardDisposalListener(disposalGui);
        //myself.registerPlayerStatusListener(new ClientPlayerStatusListener(this));
        myself.setHero(new Blank());// change in the future

        healthGui.onSetHealthLimit(myself.getHero().getHealthLimit()); // change in the future
        healthGui.onSetHealthCurrent(myself.getHero().getHealthLimit()); // change in the future
        heroGui.setHero(myself.getHero());
        confirm = new ButtonGui("Confirm", new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.getCurrentOperation().onConfirmed();
            }
        });

        confirm.setLocation(0, HEIGHT - CardRackGui.HEIGHT - ButtonGui.HEIGHT);
        cancel = new ButtonGui("Cancel", new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                panel.getCurrentOperation().onCanceled();
            }
        });
        cancel.setLocation(ButtonGui.WIDTH, HEIGHT - CardRackGui.HEIGHT - ButtonGui.HEIGHT);
        end = new ButtonGui("End", new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.getCurrentOperation().onEnded();
            }
        });
        end.setLocation(ButtonGui.WIDTH * 2, HEIGHT - CardRackGui.HEIGHT - ButtonGui.HEIGHT);

        cancelSetCount = 0;

        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        //add(cardRack);
        //add(equipmentRack);
        //add(heroGui);
        //add(healthGui);
        //add(disposalGui);
        //add(confirm);
        //add(cancel);
        //add(end);
        //add(deckSize);
        add(messageBox);
        messageBox.setMessage("hello");


        Deck deck = Deck.createDeck(Deck.DeckPack.STANDARD);
        cardRack.onCardAdded(deck.drawCard());

        JPanel jPanel = new JPanel(new BorderLayout());
        jPanel.add(cardRack, BorderLayout.CENTER);
        jPanel.add(messageBox,BorderLayout.EAST);

        add(jPanel, BorderLayout.SOUTH);
    }

    public synchronized void addPlayer(Player info) {
        PlayerSimple player = new PlayerSimple(info.getNickname(), info.getLocation());
        player.registerCardDisposalListener(disposalGui);
        PlayerGui p = new PlayerGui(player, panel);
        player.setHero(new Blank()); // remove later
        otherPlayers.add(p);
        p.setLocation(WIDTH - (otherPlayers.size()) * PlayerGui.WIDTH, 0);
        add(p);
        repaint();
    }

    @Override
    public void onPlayerAdded(PlayerSimple player) {

    }

    @Override
    public void setCardSelected(Card card, boolean selected) {

    }

    @Override
    public void setTargetSelected(Player player, boolean selected) {

    }

    @Override
    public void setCardSelectable(Card card, boolean selectable) {

    }

    @Override
    public void setTargetSelectable(Player player, boolean selectable) {

    }

    @Override
    public void setDeckSize(int size) {

    }

    @Override
    public void setMessage(String message) {

    }

    @Override
    public void clearMessage() {

    }

    @Override
    public void onDisplayCardSelectionPane(PlayerSimple player, boolean showHand, boolean showEquipments, boolean showDecisions) {

    }

    @Override
    public void onDisplayCustomizedSelectionPaneAtCenter(JPanel panel) {

    }

    @Override
    public void onRemoveCustomizedSelectionPane() {

    }

    @Override
    public void setCancelEnabled(boolean b) {

    }

    @Override
    public void setConfirmEnabled(boolean b) {

    }
}
